define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school_class_subject_schedule/index/ids/' + Config.class_subject_id + location.search,
                    add_url: 'school_class_subject_schedule/add?class_subject_id=' + Config.class_subject_id + '&class_id=' + Config.class_id,
                    edit_url: 'school_class_subject_schedule/edit',
                    del_url: 'school_class_subject_schedule/del',
                    multi_url: 'school_class_subject_schedule/multi',
                    table: 'school_class_subject_schedule',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 25,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'date', title: __('Date'), operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'time_slot', title: __('Time_slot')},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": "已结束", "2": "已调课"},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: '其他操作',
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '调课',
                                    visible:function(row){
                                        if(row.status == 0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    title: function (row) {
                                        return '调课';
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'school_class_subject_schedule/tiaoke?ids=' + row.id + '&class_id=' + Config.class_id.class_id
                                    },
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            //因为日期选择框不会触发change事件，导致无法刷新textarea，所以加上判断
            $(document).on("dp.change", "#second-form .datetimepicker", function () {
                $(this).parent().prev().find("input").trigger("change");
            });
            $(document).on("fa.event.appendfieldlist", "#second-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
                Form.events.datetimepicker(obj);
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        tiaoke: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }

    };
    return Controller;
});